package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import viewInterfaces.IViewDBConnectionSetup;
import controller.ControllerDBConnectionSetup;
import controllerInterfaces.IControllerDBConnectionSetup;

/**
 * 
 * Presents user interface for connect to database
 *
 */
public class ViewDBConnectionSetup extends JDialog implements IViewDBConnectionSetup {

	/** MVC++ Controller
	* @see ControllerDBConnectionSetup*/
	private IControllerDBConnectionSetup myController;
	
	/** URI text field*/
	private JTextField textFieldURI;
	
	/** User name text field*/
	private JTextField textFieldUser;
	
	/** Password text field*/
	private JPasswordField passwordField;


	/**
	 * Class Constructor creates the frame.
	 */
	public ViewDBConnectionSetup() {

		this.setModal(true);
		setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent arg0) {
				myController.close();
			}
		});
		setTitle("DataSet Database Connection Setup");
		setBounds(100, 100, 343, 208);
		JPanel contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblURI = new JLabel("URI:");
		
		textFieldURI = new JTextField();
		textFieldURI.setColumns(10);
		
		JLabel lblUser = new JLabel("User:");
		
		textFieldUser = new JTextField();
		textFieldUser.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password:");
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
				myController.close();
			}
		});
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				startConnect();
			}
		});
		
		passwordField = new JPasswordField();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(btnOk)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnCancel))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblPassword)
								.addComponent(lblUser)
								.addComponent(lblURI))
							.addGap(18)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(textFieldURI, GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE))
								.addComponent(passwordField, GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
								.addComponent(textFieldUser, GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE))))
					.addGap(44))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblURI)
						.addComponent(textFieldURI, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblUser)
						.addComponent(textFieldUser, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblPassword)
						.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnCancel)
						.addComponent(btnOk))
					.addContainerGap(22, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
		getRootPane().setDefaultButton(btnOk);
		this.setLocationRelativeTo(null);
	}


	/**
	 * Show the frame
	 */
	public void render() {
		this.setVisible(true);
	}

	/**
	 * Show error dialog
	 * @param code	error code
	 * @param message	error message
	 */
	public void renderError(int code, String message) {
		javax.swing.JOptionPane.showMessageDialog(this, code + " - " + message, "Error",javax.swing.JOptionPane.ERROR_MESSAGE);
	}

	
	/**
	 * Close frame
	 */
	public void close() {
		dispose();
	}


	/**
	 * Set the MVC++ Controller Class for this View.</br>
	 * ControllerDBConnectionSetup is manipulable through {@link IControllerDBConnectionSetup} interface
	 * @param	controller	MVC++ Controller
	 * @see	ControllerDBConnectionSetup
	 * @see IControllerDBConnectionSetup
	 */	
	public void setController(IControllerDBConnectionSetup controller) {
		this.myController = controller;
		
	}


	/**
	 * Set user text field
	 * @param user	database connection user name
	 */
	public void setUser(String user) {
		this.textFieldUser.setText(user);
	}


	/**
	 * Set password text field
	 * @param password	database connection user password
	 */
	public void setPassword(String password) {
		this.passwordField.setText(String.valueOf(password));
	}

	
	/**
	 * Set URI text field
	 * @param URI	database connection URI
	 */
	public void setURI(String URI) {
		this.textFieldURI.setText(URI);
	}


	/**
	 * Render status of connection according to parameter code, message and value.
	 * @param code		code of status of connection
	 * @param message	message of status of connection
	 * @param value		successful connection is true, otherwise false.
	 */
	public void renderConnectionStatus(int code, String message, boolean value) {
			if (value) {
				javax.swing.JOptionPane.showMessageDialog(this, code + " - "
						+ message, "Success",
						javax.swing.JOptionPane.INFORMATION_MESSAGE);
			} else {
				javax.swing.JOptionPane.showMessageDialog(this, code + " - "
						+ message, "Error",
						javax.swing.JOptionPane.ERROR_MESSAGE);
			}
	}
	
	
	/**
	 * Start connect to database.
	 */
	public void startConnect() {
		if (!textFieldURI.getText().isEmpty() && !textFieldUser.getText().isEmpty())
		{
			myController.connect(textFieldURI.getText(), textFieldUser.getText(), String.valueOf(passwordField.getPassword()));
		}
		else
		{
			renderError(utils.Codes.errorDBconnect, utils.Messages.errorDBconnect);
		}
		
	}	
}
